﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace BadassPhotoAlbum
{

    public enum PhotoSortOrder
    {
        Descending, Ascending
    };
    /*******************************************************************************************
    * Created by: Adam Reaves, Mike Flood and Joseph Alfred
    * This is the main class that handles the function call for albums. An album consist of
    * the list of photos within, a description, the album name and an album id. 
    * ****************************************************************************************/
    public class Album
    {
        private List<Photo> photoList;
        private string albumDescription;
        private string albumName;
        private int albumID;

        /*******************************************************************************************
         * Created by: Adam Reaves and Mike Flood
         * This is the default constructor for creating an album. When an album is created it is 
         * assigned an id, name, and description.
         * ****************************************************************************************/
        public Album(string name, string description, int id)
        {
            albumName = name;
            albumDescription = description;
            albumID = id;
            photoList = new List<Photo>();
        }

        /*******************************************************************************************
         * Created by: Adam Reaves and Mike Flood
         * This is the delet photo function. It takes the photo id as an input and searches through 
         * an album until a matching photo id is located. If there is a matching id then the photo
         * is removed from the photo list and returnes true. If there is no match then nothing happens and the function
         * is returned as false.
        * ****************************************************************************************/
        public bool deletePhoto(int id)
        {
            if (getPhotobyID(id) != null)
            {
                int photoIndex = getPhotoLocationbyID(id);
                photoList.RemoveAt(photoIndex);
                return true;
            }
            return false;
        }

        public void movePhototoIndex(int photoid, int index)
        {
            Photo modPhoto = getPhotobyID(photoid);

            if (modPhoto == null)
                return;

            if (!this.deletePhoto(photoid))
                return;

            if (index > photoList.Count)
                index = photoList.Count;

            photoList.Insert(index, modPhoto);
        }
        public void SortAlbumbyName(PhotoSortOrder order)
        {
            switch(order)
            {
                case PhotoSortOrder.Descending:
                    photoList.Sort(ComparePhotosReverseAlphabetical);
                    break;
                case PhotoSortOrder.Ascending:
                    photoList.Sort(ComparePhotosAlphabetical);
                    break;
            }
        }
        private int ComparePhotosReverseAlphabetical(Photo a, Photo b)
        {
            if (string.Compare(a.PhotoName, b.PhotoName) > 0)
                return -1;
            else if (string.Compare(a.PhotoName, b.PhotoName) < 0)
                return 1;
            else
                return 0;
        }
        private int ComparePhotosAlphabetical(Photo a, Photo b)
        {
            if (string.Compare(a.PhotoName, b.PhotoName) < 0)
                return -1;
            else if (string.Compare(a.PhotoName, b.PhotoName) > 0)
                return 1;
            else
                return 0;
        }

         /*******************************************************************************************
         * Created by: Adam Reaves and Mike Flood
         * This is the add photo function. It takes the photo id as an input. The photo is then matched 
         * against the list of photos in the album and if a match is found then false is returned. If
         *  not then the photo is added to the list and true is returned.
         * ****************************************************************************************/
        public bool addPhoto(Photo ph)
        {
            foreach (Photo p in photoList)
                if (ph.GetHashValue() == p.GetHashValue())
                    return false;

            photoList.Add(ph);
            return true;
        }

        /******************************************************************************
         * the getPhotoList() is a function that returns the list of photos in the album
         * ****************************************************************************/
        public List<Photo> getPhotoList()
        {
            return photoList;
        }

        /*******************************************************************************
         * This function takes the id of the selected album and returns the list of photos
         * in the selected album.
         * ****************************************************************************/
        public Photo getPhotobyID(int id)
        {
            foreach (Photo p in photoList)
            {
                if (p.GetPhotoID() == id)
                    return p;
            }

            return null;
        }

        /*********************************************************************************
         * This function is used to get and set an album name.
         * ******************************************************************************/
        public string AlbumName
        {
            get { return albumName; }
            set { albumName = value; }
        }

        /********************************************************************************
         * This function is sed to get and set the album description.
         * *******************************************************************************/
        public string AlbumDescription
        {
            get { return albumDescription; }
            set { albumDescription = value; }
        }

        /*******************************************************************************
         * This function is used to return the album id
         * ****************************************************************************/
        public int getAlbumID()
        {
            return albumID;
        }
        /****************************************************************************
         * This function inputs the photo id and the list of photos is traversed until
         * the index of the photo is found. If the id does not exist then -1 is retuened.
         * **************************************************************************/
        private int getPhotoLocationbyID(int id) //ID must exist!
        {
            for (int i = 0; i < photoList.Count; i++)
            {
                if (photoList[i].GetPhotoID() == id)
                    return i;
            }
            return -1; //should NOT HAPPEN if it does, this function is being used incorrectly
        }
    }
}